| Conditions | 7 |
| Paths | > 20000 |
| Total Lines | 264 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /*! |
||
| 34 | $.fn[_iCheck] = function(options, fire) { |
||
| 35 | |||
| 36 | // Walker |
||
| 37 | var handle = 'input[type="' + _checkbox + '"], input[type="' + _radio + '"]', |
||
| 38 | stack = $(), |
||
| 39 | walker = function(object) { |
||
| 40 | object.each(function() { |
||
| 41 | var self = $(this); |
||
| 42 | |||
| 43 | if (self.is(handle)) { |
||
| 44 | stack = stack.add(self); |
||
| 45 | } else { |
||
| 46 | stack = stack.add(self.find(handle)); |
||
| 47 | } |
||
| 48 | }); |
||
| 49 | }; |
||
| 50 | |||
| 51 | // Check if we should operate with some method |
||
| 52 | if (/^(check|uncheck|toggle|indeterminate|determinate|disable|enable|update|destroy)$/i.test(options)) { |
||
| 53 | |||
| 54 | // Normalize method's name |
||
| 55 | options = options.toLowerCase(); |
||
| 56 | |||
| 57 | // Find checkboxes and radio buttons |
||
| 58 | walker(this); |
||
| 59 | |||
| 60 | return stack.each(function() { |
||
| 61 | var self = $(this); |
||
| 62 | |||
| 63 | if (options == 'destroy') { |
||
| 64 | tidy(self, 'ifDestroyed'); |
||
| 65 | } else { |
||
| 66 | operate(self, true, options); |
||
| 67 | } |
||
| 68 | // Fire method's callback |
||
| 69 | if ($.isFunction(fire)) { |
||
| 70 | fire(); |
||
| 71 | } |
||
| 72 | }); |
||
| 73 | |||
| 74 | // Customization |
||
| 75 | } else if (typeof options == 'object' || !options) { |
||
| 76 | |||
| 77 | // Check if any options were passed |
||
| 78 | var settings = $.extend({ |
||
| 79 | checkedClass: _checked, |
||
| 80 | disabledClass: _disabled, |
||
| 81 | indeterminateClass: _indeterminate, |
||
| 82 | labelHover: true, |
||
| 83 | aria: false |
||
| 84 | }, options), |
||
| 85 | |||
| 86 | selector = settings.handle, |
||
| 87 | hoverClass = settings.hoverClass || 'hover', |
||
| 88 | focusClass = settings.focusClass || 'focus', |
||
| 89 | activeClass = settings.activeClass || 'active', |
||
| 90 | labelHover = !!settings.labelHover, |
||
| 91 | labelHoverClass = settings.labelHoverClass || 'hover', |
||
| 92 | |||
| 93 | // Setup clickable area |
||
| 94 | area = ('' + settings.increaseArea).replace('%', '') | 0; |
||
| 95 | |||
| 96 | // Selector limit |
||
| 97 | if (selector == _checkbox || selector == _radio) { |
||
| 98 | handle = 'input[type="' + selector + '"]'; |
||
| 99 | } |
||
| 100 | // Clickable area limit |
||
| 101 | if (area < -50) { |
||
| 102 | area = -50; |
||
| 103 | } |
||
| 104 | // Walk around the selector |
||
| 105 | walker(this); |
||
| 106 | |||
| 107 | return stack.each(function() { |
||
| 108 | var self = $(this); |
||
| 109 | |||
| 110 | // If already customized |
||
| 111 | tidy(self); |
||
| 112 | |||
| 113 | var node = this, |
||
| 114 | id = node.id, |
||
| 115 | |||
| 116 | // Layer styles |
||
| 117 | offset = -area + '%', |
||
| 118 | size = 100 + (area * 2) + '%', |
||
| 119 | layer = { |
||
| 120 | position: 'absolute', |
||
| 121 | top: offset, |
||
| 122 | left: offset, |
||
| 123 | display: 'block', |
||
| 124 | width: size, |
||
| 125 | height: size, |
||
| 126 | margin: 0, |
||
| 127 | padding: 0, |
||
| 128 | background: '#fff', |
||
| 129 | border: 0, |
||
| 130 | opacity: 0 |
||
| 131 | }, |
||
| 132 | |||
| 133 | // Choose how to hide input |
||
| 134 | hide = _mobile ? { |
||
| 135 | position: 'absolute', |
||
| 136 | visibility: 'hidden' |
||
| 137 | } : area ? layer : { |
||
| 138 | position: 'absolute', |
||
| 139 | opacity: 0 |
||
| 140 | }, |
||
| 141 | |||
| 142 | // Get proper class |
||
| 143 | className = node[_type] == _checkbox ? settings.checkboxClass || 'i' + _checkbox : settings.radioClass || 'i' + _radio, |
||
| 144 | |||
| 145 | // Find assigned labels |
||
| 146 | label = $(_label + '[for="' + id + '"]').add(self.closest(_label)), |
||
| 147 | |||
| 148 | // Check ARIA option |
||
| 149 | aria = !!settings.aria, |
||
| 150 | |||
| 151 | // Set ARIA placeholder |
||
| 152 | ariaID = _iCheck + '-' + Math.random().toString(36).replace('0.', ''), |
||
| 153 | |||
| 154 | // Parent & helper |
||
| 155 | parent = '<div class="' + className + '" ' + (aria ? 'role="' + node[_type] + '" ' : ''), |
||
| 156 | helper; |
||
| 157 | |||
| 158 | // Set ARIA "labelledby" |
||
| 159 | if (label.length && aria) { |
||
| 160 | label.each(function() { |
||
| 161 | parent += 'aria-labelledby="'; |
||
| 162 | |||
| 163 | if (this.id) { |
||
| 164 | parent += this.id; |
||
| 165 | } else { |
||
| 166 | this.id = ariaID; |
||
| 167 | parent += ariaID; |
||
| 168 | } |
||
| 169 | |||
| 170 | parent += '"'; |
||
| 171 | }); |
||
| 172 | } |
||
| 173 | // Wrap input |
||
| 174 | parent = self.wrap(parent + '/>')[_callback]('ifCreated').parent().append(settings.insert); |
||
| 175 | |||
| 176 | // Layer addition |
||
| 177 | helper = $('<ins class="' + _iCheckHelper + '"/>').css(layer).appendTo(parent); |
||
| 178 | |||
| 179 | // Finalize customization |
||
| 180 | self.data(_iCheck, {o: settings, s: self.attr('style')}).css(hide); |
||
| 181 | !!settings.inheritClass && parent[_add](node.className || ''); |
||
| 182 | !!settings.inheritID && id && parent.attr('id', _iCheck + '-' + id); |
||
| 183 | parent.css('position') == 'static' && parent.css('position', 'relative'); |
||
| 184 | operate(self, true, _update); |
||
| 185 | |||
| 186 | // Label events |
||
| 187 | if (label.length) { |
||
| 188 | label.on(_click + '.i mouseover.i mouseout.i ' + _touch, function(event) { |
||
| 189 | var type = event[_type], |
||
| 190 | item = $(this); |
||
| 191 | |||
| 192 | // Do nothing if input is disabled |
||
| 193 | if (!node[_disabled]) { |
||
| 194 | |||
| 195 | // Click |
||
| 196 | if (type == _click) { |
||
| 197 | if ($(event.target).is('a')) { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | operate(self, false, true); |
||
| 201 | |||
| 202 | // Hover state |
||
| 203 | } else if (labelHover) { |
||
| 204 | |||
| 205 | // mouseout|touchend |
||
| 206 | if (/ut|nd/.test(type)) { |
||
| 207 | parent[_remove](hoverClass); |
||
| 208 | item[_remove](labelHoverClass); |
||
| 209 | } else { |
||
| 210 | parent[_add](hoverClass); |
||
| 211 | item[_add](labelHoverClass); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | if (_mobile) { |
||
| 215 | event.stopPropagation(); |
||
| 216 | } else { |
||
| 217 | return false; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | }); |
||
| 221 | } |
||
| 222 | // Input events |
||
| 223 | self.on(_click + '.i focus.i blur.i keyup.i keydown.i keypress.i', function(event) { |
||
| 224 | var type = event[_type], |
||
| 225 | key = event.keyCode; |
||
| 226 | |||
| 227 | // Click |
||
| 228 | if (type == _click) { |
||
| 229 | return false; |
||
| 230 | |||
| 231 | // Keydown |
||
| 232 | } else if (type == 'keydown' && key == 32) { |
||
| 233 | if (!(node[_type] == _radio && node[_checked])) { |
||
| 234 | if (node[_checked]) { |
||
| 235 | off(self, _checked); |
||
| 236 | } else { |
||
| 237 | on(self, _checked); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | return false; |
||
| 241 | |||
| 242 | // Keyup |
||
| 243 | } else if (type == 'keyup' && node[_type] == _radio) { |
||
| 244 | !node[_checked] && on(self, _checked); |
||
| 245 | |||
| 246 | // Focus/blur |
||
| 247 | } else if (/us|ur/.test(type)) { |
||
| 248 | parent[type == 'blur' ? _remove : _add](focusClass); |
||
| 249 | } |
||
| 250 | }); |
||
| 251 | |||
| 252 | // Helper events |
||
| 253 | helper.on(_click + ' mousedown mouseup mouseover mouseout ' + _touch, function(event) { |
||
| 254 | var type = event[_type], |
||
| 255 | |||
| 256 | // mousedown|mouseup |
||
| 257 | toggle = /wn|up/.test(type) ? activeClass : hoverClass; |
||
| 258 | |||
| 259 | // Do nothing if input is disabled |
||
| 260 | if (!node[_disabled]) { |
||
| 261 | |||
| 262 | // Click |
||
| 263 | if (type == _click) { |
||
| 264 | operate(self, false, true); |
||
| 265 | |||
| 266 | // Active and hover states |
||
| 267 | } else { |
||
| 268 | |||
| 269 | // State is on |
||
| 270 | if (/wn|er|in/.test(type)) { |
||
| 271 | |||
| 272 | // mousedown|mouseover|touchbegin |
||
| 273 | parent[_add](toggle); |
||
| 274 | |||
| 275 | // State is off |
||
| 276 | } else { |
||
| 277 | parent[_remove](toggle + ' ' + activeClass); |
||
| 278 | } |
||
| 279 | // Label hover |
||
| 280 | if (label.length && labelHover && toggle == hoverClass) { |
||
| 281 | |||
| 282 | // mouseout|touchend |
||
| 283 | label[/ut|nd/.test(type) ? _remove : _add](labelHoverClass); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | if (_mobile) { |
||
| 287 | event.stopPropagation(); |
||
| 288 | } else { |
||
| 289 | return false; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | }); |
||
| 293 | }); |
||
| 294 | } else { |
||
| 295 | return this; |
||
| 296 | } |
||
| 297 | }; |
||
| 298 | |||
| 479 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.